This derivation of EventHandlerForControl<TEventArgs> will directly call the delegate on the current thread if the control's handle is not valid rather than raising the ObjectDisposedExcpetion.
Syntax
Type Parameters
Example
Library/Library.Test/TestDelegates.cs
C# | Copy Code |
---|
ForcedEventHandlerForControl<VerifiedReceiptEventArgs> handler;
handler = new ForcedEventHandlerForControl<VerifiedReceiptEventArgs>(_form, VerifiedReceipt);
handler = new ForcedEventHandlerForControl<VerifiedReceiptEventArgs>(_form, new VerifiedReceiptEventHandler(handler.EventHandler));
VerifiedReceiptEventArgs args = new VerifiedReceiptEventArgs();
_form.Shown += delegate(object s, EventArgs e) { System.Threading.Thread.Sleep(100); _form.Close(); };
_thread.Start();
int total = 0, countOnThread = 0, countOffThread = 0;
try
{
for (total = 1; total < 1000000; total++)
{
handler.EventHandler(null, args.Reset());
Assert.IsTrue(args.Received);
if (args.OnThread)
countOnThread++;
else
countOffThread++;
//Assert.IsTrue(args.OnThread);
//Console.Error.WriteLine("Threaded = {0}", args.OnThread);
if (_form.IsDisposed)
break;
}
Assert.AreEqual(total, countOnThread + countOffThread);
Assert.IsTrue(_form.IsDisposed);
Assert.AreNotEqual(0, countOnThread);
Assert.AreNotEqual(0, countOffThread);
}
catch (Exception e)
{
Console.Error.WriteLine(e.ToString());
throw;
}
finally
{
_thread.Join();
} |
VB.NET | Copy Code |
---|
Dim handler As ForcedEventHandlerForControl(Of VerifiedReceiptEventArgs)
handler = New ForcedEventHandlerForControl(Of VerifiedReceiptEventArgs)(_form, VerifiedReceipt)
handler = New ForcedEventHandlerForControl(Of VerifiedReceiptEventArgs)(_form, New VerifiedReceiptEventHandler(handler.EventHandler))
Dim args As New VerifiedReceiptEventArgs()
_form.Shown += Function(s As Object, e As EventArgs) Do
System.Threading.Thread.Sleep(100)
_form.Close()
End Function
_thread.Start()
Dim total As Integer = 0, countOnThread As Integer = 0, countOffThread As Integer = 0
Try
total = 1
While total < 1000000
handler.EventHandler(Nothing, args.Reset())
Assert.IsTrue(args.Received)
If args.OnThread Then
System.Math.Max(System.Threading.Interlocked.Increment(countOnThread),countOnThread - 1)
Else
System.Math.Max(System.Threading.Interlocked.Increment(countOffThread),countOffThread - 1)
End If
'Assert.IsTrue(args.OnThread);
'Console.Error.WriteLine("Threaded = {0}", args.OnThread);
If _form.IsDisposed Then
Exit While
End If
System.Math.Max(System.Threading.Interlocked.Increment(total),total - 1)
End While
Assert.AreEqual(total, countOnThread + countOffThread)
Assert.IsTrue(_form.IsDisposed)
Assert.AreNotEqual(0, countOnThread)
Assert.AreNotEqual(0, countOffThread)
Catch e As Exception
Console.[Error].WriteLine(e.ToString())
Throw
Finally
_thread.Join()
End Try |
Inheritance Hierarchy
Requirements
Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7
See Also